大盘分析订阅
生产 环境:
https://stream.valuescan.ai
一、概述
大盘分析是ValueAgent通过多维度数据,对BTC/ETH的行情进行综合分析的输出结果。大盘订阅是一种服务端主动推送机制,用户订阅后,当大盘行情发生变化时,服务端通过 SSE 通道主动推送分析文本到客户端。
二、订阅地址
GET https://stream.valuescan.ai/stream/market/subscribe鉴权参数(Query String):
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
apiKey | string | 是 | ValueScan Access Key |
sign | string | 是 | HMAC-SHA256 签名 |
timestamp | long | 是 | 毫秒级 Unix 时间戳 |
nonce | string | 是 | 随机字符串(防重放) |
签名算法:
sign = HMAC-SHA256(SK, timestamp毫秒 + nonce)三、SSE 事件格式
连接成功
event: connected
data: subscribed心跳(维持连接)
: heartbeat
event: heartbeat
data: ping服务端每 20 秒发送一次心跳。
大盘分析推送
event: market
data: {"ts":1775604300144,"uniqueId":"大盘分析_20260408103000","content":"BTC 主导市场,资金集中度高,建议关注 BTC 走势。"}market 事件 payload 字段:
| 字段 | 类型 | 说明 |
|---|---|---|
ts | long | 推送时间(毫秒时间戳) |
uniqueId | string | 唯一标识(用于去重) |
content | string | 大盘分析纯文本内容 |
四、订阅示例
Python
python
import hashlib
import hmac
import json
import time
import uuid
import urllib.request
from urllib.parse import urlencode
AK = "your-access-key"
SK = "your-secret-key"
ts = int(time.time() * 1000)
nonce = uuid.uuid4().hex
sign = hmac.new(
SK.encode(),
(str(ts) + nonce).encode(),
hashlib.sha256,
).hexdigest()
params = urlencode({
"apiKey": AK,
"sign": sign,
"timestamp": ts,
"nonce": nonce,
})
url = f"https://stream.valuescan.ai/stream/market/subscribe?{params}"
req = urllib.request.Request(url, headers={"Accept": "text/event-stream"})
with urllib.request.urlopen(req, timeout=300) as resp:
event_name = ""
data_str = ""
for raw in resp:
line = raw.decode("utf-8").rstrip("\r\n")
if line.startswith(":"):
print("♥ 心跳")
continue
if line == "":
if data_str:
handle_event(event_name, data_str)
event_name = ""
data_str = ""
elif line.startswith("event:"):
event_name = line[6:].strip()
elif line.startswith("data:"):
data_str = line[5:].strip()
def handle_event(event_name: str, data_str: str) -> None:
if event_name == "connected":
print(f"[连接] {data_str}")
elif event_name == "market":
payload = json.loads(data_str)
ts = payload["ts"]
content = payload["content"]
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts / 1000))
print(f"[大盘分析 {dt}] {content}")cURL(快速验证)
bash
python3 -c "
import hashlib, hmac, time, uuid, urllib.parse
SK='your-secret-key'
ts=int(time.time()*1000)
nonce=uuid.uuid4().hex
sign=hmac.new(SK.encode(), (str(ts)+nonce).encode(), hashlib.sha256).hexdigest()
params=urllib.parse.urlencode({'apiKey':'your-access-key','sign':sign,'timestamp':ts,'nonce':nonce})
print(f"curl -N 'https://stream.valuescan.ai/stream/market/subscribe?{params}' -H 'Accept: text/event-stream'")
'"五、断线重连
与信号订阅一致,建议实现指数退避重连:
python
def listen_with_retry(url: str, max_retries: int = 10) -> None:
retries = 0
while retries <= max_retries:
try:
req = urllib.request.Request(url, headers={"Accept": "text/event-stream"})
with urllib.request.urlopen(req, timeout=300) as resp:
_read_sse(resp)
break
except KeyboardInterrupt:
print("用户中断,退出。")
break
except Exception as e:
retries += 1
wait = min(2 ** retries, 60)
print(f"连接断开({e}),{wait}s 后第 {retries} 次重连...")
time.sleep(wait)
else:
print("重试次数耗尽,退出。")六、错误码
| HTTP 状态码 | 说明 |
|---|---|
| 200 | 连接成功 |
| 400 | 参数缺失或格式错误 |
| 401 | 签名验证失败或时间戳超限 |
| 404 | 订阅地址错误 |
| 503 | 服务端不可用(鉴权后端连接失败) |